package com.thinkingstar.iads.datacenter.entity.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.Sqls;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Criteria;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;

import com.thinkingstar.iads.common.serviceImpl.EntityRepositoryServiceImpl;
import com.thinkingstar.iads.common.utils.CommonDateUtil;
import com.thinkingstar.iads.datacenter.entity.DcEquipment;
import com.thinkingstar.iads.datacenter.entity.DcEquipmentManufacturer;

/**
 * @ClassName: DaoDcProjectDesignContent 
 * @Description:数据中心_项目_项目设备维护
 * @author 1157372219@qq.com 
 * @date 2018年3月19日   
 */
@IocBean(args = {"refer:dao"})
public class DaoDcEquipment extends EntityRepositoryServiceImpl<DcEquipment>{

	public DaoDcEquipment(Dao dao) {
		super(dao);
	}
	
	//查询项目中心已初配置设备以及未配置设备信息
	public QueryResult findPagePcEquipmentListByEquipmentTypeId(NutMap param){
		List <NutMap> page_list = new ArrayList<>();
		Pager pager = null;
		if(param == null){
			return null;
		}
		String query_type = param.getString("query_type"); //查询级别
		String equipment_type_id = param.getString("equipment_type_id"); //设备类型id
		String equip_id = param.getString("equip_id"); //设备配备id
		StringBuilder find_sql = null;
		if("equipment_type".equals(query_type)){
			//设备类型查找
			find_sql = new StringBuilder(" select de.* , "
					+ " (select ifnull(peei.equipment_num,'') from pc_equipment_equip_info peei where peei.equipment_id = de.id and peei.del_flag = '0' "
					+ " and peei.equipment_equip_id = @equip_id) equipment_num ,"
					+ " (select ifnull(peei.id,'')  from pc_equipment_equip_info peei where peei.equipment_id = de.id and peei.del_flag = '0' "
					+ " and peei.equipment_equip_id = @equip_id) equipment_equip_info_id  "
					+ " from dc_equipment_children_type dect inner join dc_equipment_type det "
					+ " on det.id = dect.equipment_type_id and det.del_flag = '0' and dect.del_flag = '0'"
					+ " inner join dc_equipment de on dect.id = de.equipment_children_type_id "
					+ " where det.id = @type_id and de.del_flag = '0' ");
		}else{
			//设备子类型查找
			find_sql = new StringBuilder(" select de.* ,"
					+ " (select ifnull(peei.equipment_num,'') from pc_equipment_equip_info peei where peei.equipment_id = de.id and peei.del_flag = '0' "
					+ " and peei.equipment_equip_id = @equip_id ) equipment_num ,"
					+ " (select ifnull(peei.id,'')  from pc_equipment_equip_info peei where peei.equipment_id = de.id and peei.del_flag = '0' "
					+ " and peei.equipment_equip_id = @equip_id) equipment_equip_info_id "
					+ " from dc_equipment_children_type dect inner join dc_equipment de on "
					+ " dect.id = de.equipment_children_type_id and dect.del_flag = '0' and de.del_flag = '0' "
					+ " where dect.id = @type_id ");
		}
		Sql sql = Sqls.create(find_sql.toString());
		sql.params().set("type_id", equipment_type_id);
		sql.params().set("equip_id", equip_id);
		if(param.get("page_num") != null && param.get("page_size") != null){
			pager = new Pager(param.getInt("page_num"), param.getInt("page_size"));
			pager.setRecordCount(findPageEquipmentListNumByEquipmentTypeId(param));
			sql.setPager(pager);
		}
		sql.setCallback(new SqlCallback() {
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
			    List<NutMap> list = new ArrayList<>();
			    while (rs.next()){
			    	NutMap map = new NutMap();
			        ResultSetMetaData metaData = rs.getMetaData();
			        int count = metaData.getColumnCount();
			        for (int i = count; i > 0; i--) {
			            String filed = metaData.getColumnName(i);
			            map.put(filed, rs.getString(filed));
			        }
			        list.add(map);
			    }
			    return list;
			}
		});
		dao().execute(sql);
		page_list = sql.getList(NutMap.class);
		return new QueryResult(page_list,pager);
	}
	
	
	public QueryResult findPageDcEquipmentListByEquipmentTypeId(NutMap param){
		List <NutMap> page_list = new ArrayList<>();
		Pager pager = null;
		if(param == null){
			return null;
		}
		String query_type = param.getString("query_type"); //查询级别
		String equipment_type_id = param.getString("equipment_type_id"); //设备类型id
		String equip_id = param.getString("equip_id"); //设备配备id
		StringBuilder find_sql = null;
		if("equipment_type".equals(query_type)){
			//设备类型查找
			find_sql = new StringBuilder(" select de.* , "
					+ " (select ifnull(deei.equipment_num,'') from dc_equipment_equip_info deei where deei.equipment_id = de.id and deei.del_flag = '0' "
					+ " and deei.equipment_equip_id = @equip_id) equipment_num ,"
					+ " (select ifnull(deei.id,'')  from dc_equipment_equip_info deei where deei.equipment_id = de.id and deei.del_flag = '0' "
					+ " and deei.equipment_equip_id = @equip_id) equipment_equip_info_id  "
					+ " from dc_equipment_children_type dect inner join dc_equipment_type det "
					+ " on det.id = dect.equipment_type_id and det.del_flag = '0' and dect.del_flag = '0'"
					+ " inner join dc_equipment de on dect.id = de.equipment_children_type_id "
					+ " where det.id = @type_id and de.del_flag = '0' ");
		}else{
			//设备子类型查找
			find_sql = new StringBuilder(" select de.* ,"
					+ " (select ifnull(deei.equipment_num,'') from dc_equipment_equip_info deei where deei.equipment_id = de.id and deei.del_flag = '0' "
					+ " and deei.equipment_equip_id = @equip_id ) equipment_num ,"
					+ " (select ifnull(deei.id,'')  from dc_equipment_equip_info deei where deei.equipment_id = de.id and deei.del_flag = '0' "
					+ " and deei.equipment_equip_id = @equip_id) equipment_equip_info_id "
					+ " from dc_equipment_children_type dect inner join dc_equipment de on "
					+ " dect.id = de.equipment_children_type_id and dect.del_flag = '0' and de.del_flag = '0' "
					+ " where dect.id = @type_id ");
		}
		Sql sql = Sqls.create(find_sql.toString());
		sql.params().set("type_id", equipment_type_id);
		sql.params().set("equip_id", equip_id);
		if(param.get("page_num") != null && param.get("page_size") != null){
			pager = new Pager(param.getInt("page_num"), param.getInt("page_size"));
			pager.setRecordCount(findPageEquipmentListNumByEquipmentTypeId(param));
			sql.setPager(pager);
		}
		sql.setCallback(new SqlCallback() {
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
			    List<NutMap> list = new ArrayList<>();
			    while (rs.next()){
			    	NutMap map = new NutMap();
			        ResultSetMetaData metaData = rs.getMetaData();
			        int count = metaData.getColumnCount();
			        for (int i = count; i > 0; i--) {
			            String filed = metaData.getColumnName(i);
			            map.put(filed, rs.getString(filed));
			        }
			        list.add(map);
			    }
			    return list;
			}
		});
		dao().execute(sql);
		page_list = sql.getList(NutMap.class);
		return new QueryResult(page_list,pager);
	}
	
	//根据设备类型id查询设备数量
	public int findPageEquipmentListNumByEquipmentTypeId(NutMap param){
		List <NutMap> page_list = new ArrayList<>();
		Pager pager = null;
		String query_type = param.getString("query_type");
		String equipment_type_id = param.getString("equipment_type_id");
		StringBuilder find_sql = null;
		if("equipment_type".equals(query_type)){
			//设备类型查找
			find_sql = new StringBuilder(" select count(de.id) equipment_num  "
					+ " from dc_equipment_children_type dect inner join dc_equipment_type det "
					+ " on det.id = dect.equipment_type_id and det.del_flag = '0' and dect.del_flag = '0'"
					+ " inner join dc_equipment de on dect.id = de.equipment_children_type_id "
					+ " where det.id = @type_id and de.del_flag = '0' ");
		}else{
			//设备子类型查找
			find_sql = new StringBuilder(" select count(de.id) equipment_num "
					+ " from dc_equipment_children_type dect inner join dc_equipment de on "
					+ " dect.id = de.equipment_children_type_id and dect.del_flag = '0' and de.del_flag = '0' "
					+ " where dect.id = @type_id ");
		}
		Sql sql = Sqls.create(find_sql.toString());
		sql.params().set("type_id", equipment_type_id);
		sql.setCallback(new SqlCallback() {
			int num = 0;
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
			    while (rs.next()){
			    	num = rs.getInt("equipment_num");
			    }
			    return num;
			}
		});
		dao().execute(sql);
		return sql.getInt();
	}
	
	
	public QueryResult findEquipmentPageList(NutMap nutMap){
		List <DcEquipment> page_list = null;
		Pager pager = null;
		Criteria criteria = Cnd.NEW();
		criteria.where().and("del_flag", "=", false);
		if(nutMap.get("code") != null){
			criteria.where().and("equipment_code", "=", nutMap.get("code"));
		}
		if(nutMap.get("name") != null){
			criteria.where().and("equipment_name", "like", "%"+nutMap.get("name")+"%");
		}
		if(nutMap.get("equipment_children_type_id") != null){
			criteria.where().and("equipment_children_type_id", "=", nutMap.get("equipment_children_type_id"));
		}
		criteria.getOrderBy().desc("create_time");
		if(nutMap.get("page_num") != null && nutMap.get("page_size") != null ){
			pager = new Pager(nutMap.getInt("page_num"),nutMap.getInt("page_size"));
			pager.setRecordCount(count(criteria));
			page_list = query(criteria, pager);
		}else{
			page_list = query(criteria);
		}
		return new QueryResult(page_list,pager);
	}
	
	public int updateEquipment(DcEquipment dcEquipment){
		if(dcEquipment == null){
			return 0;
		}
		int i = 0;
		String id = dcEquipment.getId();
		if(StringUtils.isNotBlank(id)){
			DcEquipment update_obj = fetch(id);
			update_obj.setUpdate_time(CommonDateUtil.getDateTime());
			update_obj.setEquipment_children_type_id(dcEquipment.getEquipment_children_type_id());
			update_obj.setEquipment_children_type_name(dcEquipment.getEquipment_children_type_name());
			update_obj.setEquipment_code(dcEquipment.getEquipment_code());
			update_obj.setEquipment_name(dcEquipment.getEquipment_name());
			update_obj.setEquipment_num_for_185(dcEquipment.getEquipment_num_for_185());
			i = update(update_obj);
		}else{
			dcEquipment.setCreate_time(CommonDateUtil.getDateTime());
			Object object = insert(dcEquipment);
			if(object != null){
				i = 1;
			}
		}
		return i;
	}
	

}
